package Views;

import Controllers.*;
import Models.*;
import Views.TableModels.ItemTableModel;
import byggemarked.Tools;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * @author Tanja og Erik
 */
public class SaleInfoView extends javax.swing.JFrame implements ListSelectionListener, WindowListener
{
	private Sale sale;
	private SaleCTR controller = new SaleCTR();
	private ArrayList<Item> itemsTemp = null;
	private ItemPicker ip = null;
	private Customer customerTemp = null;
	private CustomerPicker cp = null;
	private Employee employeeTemp = null;
	private EmployeePicker ep = null;

    public SaleInfoView(Sale sale)
	{
		this.sale = sale;
		this.itemsTemp = sale.getItems();

		initComponents();

		priceSP.setValue(this.sale.getTotal());

		if (this.sale.getDate() != null)
		{ dateTF.setText(Tools.makeNiceStringFromDate(this.sale.getDate())); }
		else
		{ dateTF.setText(""); }

		customerTF.setText(Tools.getNameOrCompanyName(this.sale.getCustomer()));
		employeeTF.setText(this.sale.getEmployee().getName());

		if (this.sale.isDelivery())
		{ deliveryYesRB.setSelected(true); }
		else
		{ deliveryNoRB.setSelected(true); }

		if (this.sale.isDelivered())
		{ deliveredYesRB.setSelected(true); }
		else
		{ deliveredNoRB.setSelected(true); }

		if (this.sale.isPaid())
		{ paidYesRB.setSelected(true); }
		else
		{ paidNoRB.setSelected(true); }

		itemTable.setModel(new ItemTableModel(this.itemsTemp));
		itemTable.getSelectionModel().addListSelectionListener(this);
		itemTable.addMouseListener(new MouseAdapter(){
			@Override
			public void mouseClicked(MouseEvent e){
				if (e.getClickCount() == 2)
				{
					if(itemTable.getSelectedRowCount() == 1)
					{
						int chosenId = Integer.parseInt(itemTable.getValueAt(itemTable.getSelectedRow(), 0).toString());
						ItemCTR itemController = new ItemCTR();
						new ItemInfoView(itemController.getItem(chosenId)).setVisible(true);
					}
				}
			}
		 });

		if(!sale.isActivated())
		{ activatedCB.setSelected(false); }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        deliveryBG = new javax.swing.ButtonGroup();
        deliveredBG = new javax.swing.ButtonGroup();
        paidBG = new javax.swing.ButtonGroup();
        saleLabel = new javax.swing.JLabel();
        activatedCB = new javax.swing.JCheckBox();
        totalPriceLabel = new javax.swing.JLabel();
        priceSP = new javax.swing.JSpinner();
        totalPriceKrLabel = new javax.swing.JLabel();
        dateLabel = new javax.swing.JLabel();
        dateTF = new javax.swing.JTextField();
        customerLabel = new javax.swing.JLabel();
        customerTF = new javax.swing.JTextField();
        findCustomerButton = new javax.swing.JButton();
        employeeLabel = new javax.swing.JLabel();
        employeeTF = new javax.swing.JTextField();
        findEmployeeButton = new javax.swing.JButton();
        deliveryLabel = new javax.swing.JLabel();
        deliveryYesRB = new javax.swing.JRadioButton();
        deliveryNoRB = new javax.swing.JRadioButton();
        deliveredLabel = new javax.swing.JLabel();
        deliveredYesRB = new javax.swing.JRadioButton();
        deliveredNoRB = new javax.swing.JRadioButton();
        itemLabel = new javax.swing.JLabel();
        ScrollPane = new javax.swing.JScrollPane();
        itemTable = new javax.swing.JTable();
        addButton = new javax.swing.JButton();
        removeButton = new javax.swing.JButton();
        editButton = new javax.swing.JButton();
        closeButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        paidYesRB = new javax.swing.JRadioButton();
        paidNoRB = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Salgsinformation");

        saleLabel.setText("Oversigt over det valgte salg");

        activatedCB.setSelected(true);
        activatedCB.setText("Aktiveret");
        activatedCB.setEnabled(false);

        totalPriceLabel.setText("Total pris:");

        priceSP.setEnabled(false);

        totalPriceKrLabel.setText("kr.");

        dateLabel.setText("Dato:");

        dateTF.setEnabled(false);

        customerLabel.setText("Kunde:");

        customerTF.setEnabled(false);

        findCustomerButton.setText("Find");
        findCustomerButton.setEnabled(false);
        findCustomerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findCustomerButtonActionPerformed(evt);
            }
        });

        employeeLabel.setText("Ansat:");

        employeeTF.setEnabled(false);

        findEmployeeButton.setText("Find");
        findEmployeeButton.setEnabled(false);
        findEmployeeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findEmployeeButtonActionPerformed(evt);
            }
        });

        deliveryLabel.setText("Levering:");

        deliveryBG.add(deliveryYesRB);
        deliveryYesRB.setText("Ja");
        deliveryYesRB.setEnabled(false);

        deliveryBG.add(deliveryNoRB);
        deliveryNoRB.setSelected(true);
        deliveryNoRB.setText("Nej");
        deliveryNoRB.setEnabled(false);

        deliveredLabel.setText("Leveret:");

        deliveredBG.add(deliveredYesRB);
        deliveredYesRB.setSelected(true);
        deliveredYesRB.setText("Ja");
        deliveredYesRB.setEnabled(false);

        deliveredBG.add(deliveredNoRB);
        deliveredNoRB.setText("Nej");
        deliveredNoRB.setEnabled(false);

        itemLabel.setText("Varer:");

        ScrollPane.setViewportView(itemTable);

        addButton.setText("Tilføj");
        addButton.setEnabled(false);
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        removeButton.setText("Fjern");
        removeButton.setEnabled(false);
        removeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeButtonActionPerformed(evt);
            }
        });

        editButton.setText("Rediger");
        editButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editButtonActionPerformed(evt);
            }
        });

        closeButton.setText("Luk");
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });

        jLabel1.setText("Betalt:");

        paidBG.add(paidYesRB);
        paidYesRB.setText("Ja");
        paidYesRB.setEnabled(false);

        paidBG.add(paidNoRB);
        paidNoRB.setSelected(true);
        paidNoRB.setText("Nej");
        paidNoRB.setEnabled(false);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(employeeLabel)
                            .add(customerLabel))
                        .add(18, 18, 18)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(customerTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(findCustomerButton))
                            .add(layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                    .add(layout.createSequentialGroup()
                                        .add(deliveryYesRB)
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                        .add(deliveryNoRB))
                                    .add(employeeTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                        .add(layout.createSequentialGroup()
                                            .add(paidYesRB)
                                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                            .add(paidNoRB))
                                        .add(layout.createSequentialGroup()
                                            .add(deliveredYesRB)
                                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                            .add(deliveredNoRB))))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(findEmployeeButton))))
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(totalPriceLabel)
                            .add(dateLabel))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(dateTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(layout.createSequentialGroup()
                                .add(priceSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(totalPriceKrLabel))))
                    .add(deliveredLabel)
                    .add(deliveryLabel)
                    .add(layout.createSequentialGroup()
                        .add(saleLabel)
                        .add(212, 212, 212)
                        .add(activatedCB))
                    .add(jLabel1)
                    .add(itemLabel)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(ScrollPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 369, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(editButton))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(closeButton)
                            .add(removeButton)
                            .add(addButton))))
                .addContainerGap(18, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(saleLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(totalPriceLabel)
                            .add(priceSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(totalPriceKrLabel))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(dateLabel)
                            .add(dateTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(customerTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(findCustomerButton)
                            .add(customerLabel))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(employeeTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(employeeLabel)
                            .add(findEmployeeButton))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(deliveryLabel)
                            .add(deliveryYesRB)
                            .add(deliveryNoRB))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(deliveredLabel)
                            .add(deliveredYesRB)
                            .add(deliveredNoRB)))
                    .add(activatedCB))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(paidYesRB)
                    .add(paidNoRB))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(itemLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(addButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(removeButton))
                    .add(layout.createSequentialGroup()
                        .add(ScrollPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 105, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(editButton)
                            .add(closeButton))))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void findCustomerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_findCustomerButtonActionPerformed
		cp = new CustomerPicker();
		cp.addWindowListener(this);
		cp.setVisible(true);
}//GEN-LAST:event_findCustomerButtonActionPerformed

	private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
		this.dispose();
	}//GEN-LAST:event_closeButtonActionPerformed

	private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
		ip = new ItemPicker();
		ip.addWindowListener(this);
		ip.setVisible(true);
	}//GEN-LAST:event_addButtonActionPerformed

	private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
		// TODO add your handling code here:
}//GEN-LAST:event_removeButtonActionPerformed

	private void editButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editButtonActionPerformed
		if (editButton.getText().equals("Rediger"))
		{
			priceSP.setEnabled(true);
			dateTF.setText(Tools.makeStringFromDate(this.sale.getDate()));
			dateTF.setEnabled(true);
			findCustomerButton.setEnabled(true);
			findEmployeeButton.setEnabled(true);
			deliveryYesRB.setEnabled(true);
			deliveryNoRB.setEnabled(true);
			deliveredYesRB.setEnabled(true);
			deliveredNoRB.setEnabled(true);
			paidYesRB.setEnabled(true);
			paidNoRB.setEnabled(true);
			addButton.setEnabled(true);
			editButton.setText("Gem");
			closeButton.setText("Annuller");
		}
		else if (editButton.getText().equals("Gem"))
		{
			this.sale.setTotal(Double.parseDouble(priceSP.getValue().toString()));

			if(!dateTF.getText().equals(""))
			{
				this.sale.setDate(Tools.makeDateFromString(dateTF.getText()));
				dateTF.setText(Tools.makeNiceStringFromDate(this.sale.getDate()));
			}
			else
			{
				this.sale.setDate(null);
				dateTF.setText("Ikke dateret endnu.");
			}
			
			this.sale.setCustomer(this.customerTemp);
			this.sale.setEmployee(this.employeeTemp);

			if (deliveryYesRB.isSelected())
			{ this.sale.setDelivery(true); }
			else if (deliveryNoRB.isSelected())
			{ this.sale.setDelivery(false); }

			if (deliveredYesRB.isSelected())
			{ this.sale.setDelivered(true); }
			else if (deliveredNoRB.isSelected())
			{ this.sale.setDelivered(false); }

			if (paidYesRB.isSelected())
			{ this.sale.setPaid(true); }
			else if (paidNoRB.isSelected())
			{ this.sale.setPaid(false); }

			this.sale.setItems(this.itemsTemp);
			priceSP.setEnabled(false);
			dateTF.setText(Tools.makeNiceStringFromDate(this.sale.getDate()));
			dateTF.setEnabled(false);
			findCustomerButton.setEnabled(false);
			findEmployeeButton.setEnabled(false);
			deliveryYesRB.setEnabled(false);
			deliveryNoRB.setEnabled(false);
			deliveredYesRB.setEnabled(false);
			deliveredNoRB.setEnabled(false);
			paidYesRB.setEnabled(false);
			paidNoRB.setEnabled(false);
			addButton.setEnabled(false);
			editButton.setText("Rediger");
			closeButton.setText("Luk");
		}
}//GEN-LAST:event_editButtonActionPerformed

	private void findEmployeeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_findEmployeeButtonActionPerformed
		ep = new EmployeePicker();
		ep.addWindowListener(this);
		ep.setVisible(true);
	}//GEN-LAST:event_findEmployeeButtonActionPerformed

	public void valueChanged(ListSelectionEvent e)
	{
		if(itemTable.getSelectedRowCount() == 1)
		{
			addButton.setEnabled(true);
		}
		else
		{
			removeButton.setEnabled(false);
		}
	}

	public void windowClosing(WindowEvent e)
	{
		if(cp != null)
		{
			this.customerTemp = cp.getChosenCustomer();
			if (this.customerTemp != null)
			{ customerTF.setText(Tools.getNameOrCompanyName(customerTemp)); }
			cp = null;
		}
		else if(ep != null)
		{
			this.employeeTemp = ep.getChosenEmployee();
			if (this.employeeTemp != null)
			{ employeeTF.setText(this.employeeTemp.getName()); }
			ep = null;
		}
		else if(ip != null)
		{
			this.itemsTemp.add(ip.getChosenItem());
			ep = null;
		}
	}
	public void windowOpened(WindowEvent e) {}
	public void windowClosed(WindowEvent e) {}
	public void windowIconified(WindowEvent e) {}
	public void windowDeiconified(WindowEvent e) {}
	public void windowActivated(WindowEvent e) {}
	public void windowDeactivated(WindowEvent e) {}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane ScrollPane;
    private javax.swing.JCheckBox activatedCB;
    private javax.swing.JButton addButton;
    private javax.swing.JButton closeButton;
    private javax.swing.JLabel customerLabel;
    private javax.swing.JTextField customerTF;
    private javax.swing.JLabel dateLabel;
    private javax.swing.JTextField dateTF;
    private javax.swing.ButtonGroup deliveredBG;
    private javax.swing.JLabel deliveredLabel;
    private javax.swing.JRadioButton deliveredNoRB;
    private javax.swing.JRadioButton deliveredYesRB;
    private javax.swing.ButtonGroup deliveryBG;
    private javax.swing.JLabel deliveryLabel;
    private javax.swing.JRadioButton deliveryNoRB;
    private javax.swing.JRadioButton deliveryYesRB;
    private javax.swing.JButton editButton;
    private javax.swing.JLabel employeeLabel;
    private javax.swing.JTextField employeeTF;
    private javax.swing.JButton findCustomerButton;
    private javax.swing.JButton findEmployeeButton;
    private javax.swing.JLabel itemLabel;
    private javax.swing.JTable itemTable;
    private javax.swing.JLabel jLabel1;
    private javax.swing.ButtonGroup paidBG;
    private javax.swing.JRadioButton paidNoRB;
    private javax.swing.JRadioButton paidYesRB;
    private javax.swing.JSpinner priceSP;
    private javax.swing.JButton removeButton;
    private javax.swing.JLabel saleLabel;
    private javax.swing.JLabel totalPriceKrLabel;
    private javax.swing.JLabel totalPriceLabel;
    // End of variables declaration//GEN-END:variables
}
